﻿using Lanbt.Core.Entity.Constant;
using Lanbt.Core.Entity.Enums;
using Lanbt.Core.Service.Helper;
using Lanbt.Core.Service.Quartz.Job.Model;
using Lanbt.Core.Utility.Extent;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace Lanbt.Core.Service.Quartz.Job
{
    public class MailJob : JobBase<LogMailModel>
    {
        public MailJob() : base(new LogMailModel())
        {
        }

        public override async Task NextExecute(IJobExecutionContext context)
        {
            var dataMap = context.MergedJobDataMap;

            var receviedEmails = dataMap.GetString(JobParamConstant.RECEIVEEMAILS)!;
            var title = dataMap.GetString(JobParamConstant.TITLE);
            var content = dataMap.GetString(JobParamConstant.CONTENT);

            LogInfo.ReceviedEmails = receviedEmails;
            LogInfo.Title = title;
            LogInfo.Content = content;

            try
            {
                await EmailHelper.Send(receviedEmails.LanbtSplit(',').ToList(), title, content);
            }
            catch (Exception ex)
            {
                await ErrorAsync(LogInfo.JobName, ex, LogInfo.ToJson(), MailLevel);
            }
        }
    }
}
